﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using System.Collections;
using System.IO;

namespace DreametryMIS
{
    public partial class frmAdmin : Form
    {

        int status_id;
        int id;
        String nick;
        public frmAdmin()
        {
            InitializeComponent();
        }

        private void frmAdmin_Load(object sender, EventArgs e)
        {
            ClassFlow.global_count = "SELECT Count(*) FROM EmploeeTask";
            ClassFlow.unique_SQL_String = "SELECT * FROM EmploeeTask";
            //this.flowControl2.getAll();

            panel4.Dock = DockStyle.Fill;
            panel4.Visible = true;
            panel6.Visible = false;
            panel2.Visible = false;
            pnlCreateAssTask.Visible = false;

            getAll();
            getStatus();
            dateTimePicker2.Value = DateTime.Now;
            dateTimePicker1.Value = DateTime.Now;
            //dateTimePicker1.Value = DateTime.Now.AddDays(-1);
            button5.PerformClick();
            label1.Text = ClassFlow.global_fullname;

            //get store data
            var fileStream = new FileStream(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\logs\Res\str.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.Default))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    cmbStore.Items.Add(line);
                }
            }

            //assigned task combobox names
            string commandText = "SELECT * From Employee";

            using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection))
            {
                DataTable users = new DataTable();
                adapter.Fill(users);

                comboBox3.DisplayMember = "Name";
                comboBox3.ValueMember = "emp_id";
                comboBox3.DataSource = users;

                DataRow dr = users.NewRow();
                dr["Name"] = "-Select a name-";
                dr["emp_id"] = 0;

                users.Rows.InsertAt(dr, 0);
                comboBox3.SelectedIndex = 0;
            }

        }

        private void getAll()
        {
            string commandText = "SELECT * From Employee";

            using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection))
            {
                DataTable users = new DataTable();
                adapter.Fill(users);

                comboBox1.DisplayMember = "Name";
                comboBox1.ValueMember = "emp_id";
                comboBox1.DataSource = users;

                DataRow dr = users.NewRow();
                dr["Name"] = "All";
                dr["emp_id"] = 0;

                users.Rows.InsertAt(dr, 0);
                comboBox1.SelectedIndex = 0;
            }

        }

        private void getStatus()
        {
            string commandText = "SELECT * From WorkFlow";

            using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection))
            {
                DataTable users = new DataTable();
                adapter.Fill(users);

                comboBox2.DisplayMember = "flowStatus";
                comboBox2.ValueMember = "flow_id";
                comboBox2.DataSource = users;

                DataRow dr = users.NewRow();
                dr["flowStatus"] = "All";
                dr["flow_id"] = 0;
                

                users.Rows.InsertAt(dr, 0);
                comboBox2.SelectedIndex = 0;

                dr = users.NewRow();
                dr["flowStatus"] = "Unfulfilled";
                dr["flow_id"] = 11;

                users.Rows.InsertAt(dr, 11);
                comboBox2.SelectedIndex = 0;
            }

        }
        private void getStatusID()
        {
            string commandText = "SELECT flow_id FROM WorkFlow WHERE flowStatus LIKE '%' + @Name + '%'";

            using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = comboBox2.Text;
                try
                {
                    command.Connection.Open();
                    status_id = Convert.ToInt32(command.ExecuteScalar().ToString());
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }
            }

        }


        bool mouseClicked = false;




        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseClicked = true;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseClicked = false;
            if (panel1.Height <= 150)
            {
                panel1.Height = 150;
            }
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            getStatusID();
            label7.Text = status_id.ToString();
        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {

        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }
        //comment coment
        //
        private void button5_Click(object sender, EventArgs e)
        {
            panel4.Dock = DockStyle.Fill;
            panel4.Visible = true;
            panel6.Visible = false;
            panel2.Visible = false;
            pnlCreateAssTask.Visible = false;
            if (comboBox2.Text == "Unfulfilled") {
                if (dateTimePicker1.Value.ToShortDateString() == dateTimePicker2.Value.ToShortDateString()) {
                    ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE date = '" + dateTimePicker1.Value.ToShortDateString() + "' and flow_id < 9";
                    ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE date = '" + dateTimePicker1.Value.ToShortDateString() + "'and flow_id < 9";

                }
                else {
                    ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' and flow_id < 9";
                    ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' and flow_id < 9";
                }
            }
            else if (dateTimePicker1.Value.ToShortDateString() == dateTimePicker2.Value.ToShortDateString())
            {
                if (textBox1.Text == "" && comboBox1.Text == "All" && comboBox2.Text == "All")
                {
                    ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE date = '" + dateTimePicker1.Value.ToShortDateString() + "' and flow_id < 10";
                    ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE date = '" + dateTimePicker1.Value.ToShortDateString() + "'and flow_id < 10";
                }
                else
                {
                    if ((textBox1.Text != "" && comboBox1.Text != "All") || (textBox1.Text != "" && comboBox2.Text != "All"))
                    {
                        if (comboBox1.Text != "All")
                        {
                            if (comboBox2.Text != "All")
                            {
                                ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' OR task_name LIKE '%" + textBox1.Text + "%'";
                                ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' OR task_name LIKE '%" + textBox1.Text + "%'";
                            }
                            else
                            {
                                ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10 OR task_name LIKE '%" + textBox1.Text + "%'";
                                ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10 OR task_name LIKE '%" + textBox1.Text + "%'";
                            }
                        }
                        else
                        {
                            ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND flow_id = '" + status_id + "'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND OR task_name LIKE '%" + textBox1.Text + "%'";
                            ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND flow_id = '" + status_id + "'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND OR task_name LIKE '%" + textBox1.Text + "%'";
                        }
                    }
                    else if (textBox1.Text != "")
                    {
                        ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' OR task_name LIKE '%" + textBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10";
                        ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' OR task_name LIKE '%" + textBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10";
                    }
                    else if (comboBox1.Text != "All")
                    {
                        if (comboBox2.Text != "All")
                        {
                            ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "'";
                            ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "'";
                        }
                        else
                        {
                            ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10";
                            ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10";
                        }
                    }
                    else
                    {
                        ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "'";
                        ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "'";
                    }
                }

            }
            else if (textBox1.Text == "" && comboBox1.Text == "All" && comboBox2.Text == "All")
            {
                ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE date >= '" + dateTimePicker1.Value.ToShortDateString() + "'AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
                ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE date >= '" + dateTimePicker1.Value.ToShortDateString() + "'AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
            }
            else
            {
                if ((textBox1.Text != "" && comboBox1.Text != "All") || (textBox1.Text != "" && comboBox2.Text != "All"))
                {
                    if (comboBox1.Text != "All")
                    {
                        if (comboBox2.Text != "All")
                        {
                            ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%'AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' OR task_name LIKE '%" + textBox1.Text + "%'";
                            ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%'AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' OR task_name LIKE '%" + textBox1.Text + "%'";
                        }
                        else
                        {
                            ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'AND Name LIKE '%" + comboBox1.Text + "%' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10 OR task_name LIKE ' %" + textBox1.Text + "%'";
                            ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'AND Name LIKE '%" + comboBox1.Text + "%' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10 OR task_name LIKE ' %" + textBox1.Text + "%'";
                        }
                    }
                    else
                    {
                        ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' OR task_name LIKE ' %" + textBox1.Text + "%'";
                        ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' OR task_name LIKE ' %" + textBox1.Text + "%'";
                    }
                }
                else if (textBox1.Text != "")
                {
                    ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'OR task_name LIKE '%" + textBox1.Text + "%' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
                    ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'OR task_name LIKE '%" + textBox1.Text + "%' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
                }
                else if (comboBox1.Text != "All")
                {
                    if (comboBox2.Text != "All")
                    {
                        ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "'";
                        ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "'";
                    }
                    else
                    {
                        ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%'AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
                        ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%'AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
                    }
                }
                else
                {
                    ClassFlow.global_count = "SELECT Count(*) FROM ViewAdmin WHERE flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "'";
                    ClassFlow.unique_SQL_String = "SELECT * FROM ViewAdmin WHERE flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "'";
                }
            }
            this.flowControl2.getAll();
        }


        private void returnMainToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            MainForm main = new MainForm();
            main.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            MainForm main = new MainForm();
            main.Close();
        }

        private void reportsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel2.Dock = DockStyle.Fill;
            panel4.Visible = false;
            panel6.Visible = false;
            panel2.Visible = true;
            pnlCreateAssTask.Visible = false;
            string commandText;

            //  commandText = "SELECT * FROM ViewAdmin";

            if (comboBox2.Text == "Unfulfilled")
            {
                if (dateTimePicker1.Value.ToShortDateString() == dateTimePicker2.Value.ToShortDateString())
                {
                    commandText = "SELECT * FROM ViewAdmin WHERE date = '" + dateTimePicker1.Value.ToShortDateString() + "'and flow_id < 9";

                }
                else
                {
                    commandText = "SELECT * FROM ViewAdmin WHERE date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' and flow_id < 9";
                }
            }
            else if (dateTimePicker1.Value.ToShortDateString() == dateTimePicker2.Value.ToShortDateString())
            {
                if (textBox1.Text == "" && comboBox1.Text == "All" && comboBox2.Text == "All")
                {
                    commandText = "SELECT * FROM ViewAdmin WHERE date = '" + dateTimePicker1.Value.ToShortDateString() + "'and flow_id < 10";
                }
                else
                {
                    if ((textBox1.Text != "" && comboBox1.Text != "All") || (textBox1.Text != "" && comboBox2.Text != "All"))
                    {
                        if (comboBox1.Text != "All")
                        {
                            if (comboBox2.Text != "All")
                            {
                                commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' OR task_name LIKE '%" + textBox1.Text + "%'";
                            }
                            else
                            {
                                commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10 OR task_name LIKE '%" + textBox1.Text + "%'";
                            }
                        }
                        else
                        {
                            commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND flow_id = '" + status_id + "'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND OR task_name LIKE '%" + textBox1.Text + "%'";
                        }
                    }
                    else if (textBox1.Text != "")
                    {
                        commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' OR task_name LIKE '%" + textBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10";
                    }
                    else if (comboBox1.Text != "All")
                    {
                        if (comboBox2.Text != "All")
                        {
                            commandText = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "'";
                        }
                        else
                        {
                            commandText = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%'AND date = '" + dateTimePicker1.Value.ToShortDateString() + "' AND flow_id < 10";
                        }
                    }
                    else
                    {
                        commandText = "SELECT * FROM ViewAdmin WHERE flow_id = '" + status_id + "' AND date = '" + dateTimePicker1.Value.ToShortDateString() + "'";
                    }
                }

            }
            else if (textBox1.Text == "" && comboBox1.Text == "All" && comboBox2.Text == "All")
            {
                commandText = "SELECT * FROM ViewAdmin WHERE date >= '" + dateTimePicker1.Value.ToShortDateString() + "'AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
            }
            else
            {
                if ((textBox1.Text != "" && comboBox1.Text != "All") || (textBox1.Text != "" && comboBox2.Text != "All"))
                {
                    if (comboBox1.Text != "All")
                    {
                        if (comboBox2.Text != "All")
                        {
                            commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%' AND Name LIKE '%" + comboBox1.Text + "%'AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' OR task_name LIKE '%" + textBox1.Text + "%'";
                        }
                        else
                        {
                            commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'AND Name LIKE '%" + comboBox1.Text + "%' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10 OR task_name LIKE ' %" + textBox1.Text + "%'";
                        }
                    }
                    else
                    {
                        commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' OR task_name LIKE ' %" + textBox1.Text + "%'";
                    }
                }
                else if (textBox1.Text != "")
                {
                    commandText = "SELECT * FROM ViewAdmin WHERE task_description LIKE '%" + textBox1.Text + "%'OR task_name LIKE '%" + textBox1.Text + "%' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
                }
                else if (comboBox1.Text != "All")
                {
                    if (comboBox2.Text != "All")
                    {
                        commandText = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%' AND flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "'";
                    }
                    else
                    {
                        commandText = "SELECT * FROM ViewAdmin WHERE Name LIKE '%" + comboBox1.Text + "%'AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "' AND flow_id < 10";
                    }
                }
                else
                {
                    commandText = "SELECT * FROM ViewAdmin WHERE flow_id = '" + status_id + "' AND date >= '" + dateTimePicker1.Value.ToShortDateString() + "' AND date <='" + dateTimePicker2.Value.ToShortDateString() + "'";
                }
            }


            using (SqlConnection con = new SqlConnection(ClassFlow.global_con))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, con))
            {
                EmployeeInfoDataSet ds = new EmployeeInfoDataSet();
                ds.ViewAdmin.Clear();
                ds.EnforceConstraints = false;
                adapter.Fill(ds.ViewAdmin);
                reportsForAdmin report = new reportsForAdmin();
                report.SetDataSource(ds);
                report.SetParameterValue("cur2", dateTimePicker2.Value);
                report.SetParameterValue("Text", "Fullfilment Report");
                crystalReportViewer1.ReportSource = report;
                
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel6.Dock = DockStyle.Fill;
            panel4.Visible = false;
            panel6.Visible = true;
            panel2.Visible = false;
            pnlCreateAssTask.Visible = false;
            String commandText = "SELECT * FROM ViewAdmin Where flow_id < 10";
            using (SqlConnection con = new SqlConnection(ClassFlow.global_con))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, con))
            {
                EmployeeInfoDataSet ds = new EmployeeInfoDataSet();
                ds.ViewAdmin.Clear();
                ds.EnforceConstraints = false;
                adapter.Fill(ds.ViewAdmin);
                ReportsForEmployees report = new ReportsForEmployees();
                report.SetDataSource(ds);
                report.SetParameterValue("cur2", DateTime.Now.Date);
                report.SetParameterValue("text", "Task Reports");

                crystalReportViewer2.ReportSource = report;
          
            }

         
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            MainForm frmMain = new MainForm();
            frmMain.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
            MainForm frmMain = new MainForm();
            frmMain.Show();
        }

        private void btnCreateTaskCancel_Click(object sender, EventArgs e)
        {
         
            button3.BackColor = Color.WhiteSmoke;
            pnlCreateAssTask.Visible = false;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            button3.BackColor = Color.PaleVioletRed;
            pnlCreateAssTask.Visible = true;
            pnlCreateAssTask.BringToFront();
        }

        private void btnCreateTask_Click(object sender, EventArgs e)
        {
            if (comboBox3.Text == "-Select a name-")
            {
                MessageBox.Show("Please select a name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if(txtTaskName.Text == "" || cmbCategory.Text == "" || txtDescription.Text == "")
            {
                MessageBox.Show("Please fill all the fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (txtTaskName.Text.Contains(@"\") || txtTaskName.Text.Contains("/") || txtTaskName.Text.Contains(":") || txtTaskName.Text.Contains("*") || txtTaskName.Text.Contains("?") || txtTaskName.Text.Contains("\"") || txtTaskName.Text.Contains("<") || txtTaskName.Text.Contains(">") || txtTaskName.Text.Contains("|") || txtTaskName.Text.Contains("'") || txtTaskName.Text.Contains("http://") || txtTaskName.Text.Contains("www.") || txtTaskName.Text.Contains(".com"))
            {
                MessageBox.Show("Please remove the special characters in the title or the title has a link", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String dataCount = "SELECT Count(*) FROM TaskTable WHERE task_name ='" + txtTaskName.Text + "'";
                String x;
                using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
                {
                    SqlCommand command = new SqlCommand(dataCount, connection);
                    command.Connection.Open();
                    x = command.ExecuteScalar().ToString();
                }
                if (x == "0")
                {
                    String rawDesc = txtDescription.Text;
                    String finaDesc = rawDesc.Replace(Environment.NewLine, "<br>");
                    String desc = "<a href=" + txtCodigaLink.Text.Replace("https://", "") + ">" + cmbStore.Text + "</a><br><br>" + finaDesc;

                    String addData;

                    if (cmbCategory.Text == "Design")
                    {
                        addData = "INSERT INTO TaskTable (task_name, task_description, date, flow_id, emp_id) VALUES(@name, @desc, @date, 1, " + id + ")";
                    }
                    else
                    {
                        addData = "INSERT INTO TaskTable (task_name, task_description, date, flow_id, emp_id) VALUES(@name, @desc, @date, 7, " + id + ")";
                    }

                    using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
                    {
                        SqlCommand command = new SqlCommand(addData, connection);
                        command.CommandType = CommandType.Text;
                        command.Parameters.AddWithValue("@name", txtTaskName.Text);
                        command.Parameters.AddWithValue("@desc", desc);
                        command.Parameters.AddWithValue("@date", DateTime.Now);

                        command.Connection.Open();
                        command.ExecuteNonQuery();
                        MessageBox.Show("Task has been created", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        command.Connection.Close();
                    }

                    writeFile.Main(txtCodigaLink.Text + Environment.NewLine + Environment.NewLine + ClassFlow.line + Environment.NewLine + Environment.NewLine + "Date: " + DateTime.Now + Environment.NewLine + "Task name: " + txtTaskName.Text + Environment.NewLine + "Assigned by: " + ClassFlow.global_fullname + "Task Assigned" + Environment.NewLine + Environment.NewLine + ClassFlow.line + Environment.NewLine, @"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\" + nick + @"\logs\" + txtTaskName.Text + @".txt");
                    writeFile.Main("Date: " + DateTime.Now + Environment.NewLine + "Task name: " + txtTaskName.Text + Environment.NewLine + "Designer: " + ClassFlow.global_fullname + "Task Assigned", @"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Deo\C#\Backup Files\Verify Logs\logs.txt");
                    txtTaskName.Clear();
                    txtCodigaLink.Clear();
                    txtDescription.Clear();
                    btnCreateTaskCancel.PerformClick();
                }
                else
                {
                    MessageBox.Show("Task already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            // id
            string commandText = "SELECT emp_id FROM Employee WHERE Name LIKE '%' + @Name + '%'";

            using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = comboBox3.Text;
                try
                {
                    command.Connection.Open();
                    id = Int32.Parse(command.ExecuteScalar().ToString());
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }
            }

            string commandText2 = "SELECT taskInProgress FROM Employee WHERE Name LIKE '%' + @Name + '%'";

            using (SqlConnection connection = new SqlConnection(ClassFlow.global_con))
            {
                SqlCommand command = new SqlCommand(commandText2, connection);
                command.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = comboBox3.Text;
                try
                {
                    command.Connection.Open();
                    nick = command.ExecuteScalar().ToString();
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }
            }
        }

       
    }
}